/**
 *
 * @author pedrolopes
 *
 * GEN2PHEN Semantic Web Labs: Knowledge Mashup
 *
 */

/**
 * Perform AJAX request to get all search terms for a given investigation (Description + Features).
 */
jQuery.investigationSearch = function(query) {
    var address = 'investigation/search/' + query;

    var response = $.ajax({
        url: address,
        type : 'GET',
        async: false
    }).responseText;

    return response;
}

/**
 * Perform an AJAX Freebase search, loading and parsing the JSON result
 */
jQuery.freebaseSearch = function(query) {
    var address = 'freebase/search/' + query;
    var response = $.ajax({
        url: address,
        async : false,
        type : 'GET'
    }).responseText;
    
    var result = "";
    result += response;
    var obj = result.parseJSON();
    var results = jsonPath(obj, "$..guid");     // JSONPath to obtain all valid Freebase GUIDs

    var i = 0;
    while (results[i] != undefined || i < 10) {
        $.freebaseGet(results[i].replace("#",""));  // get each GUID value
        i = i + 1;
    }
}

/**
 * Get Freebase GUID values using AJAX, and add them to the DOM
 */
jQuery.freebaseGet = function(id) {
    var address = 'freebase/get/' + id;
 
    $.ajax({
        url: address,
        success: function(data) {
            if (data != "") {
                html = "<div class=\"free\">" + data + "</div>";
                $('#freebase').append(html);
            }
        }
    });    
}

/**
 * Perform an AJAX Flickr search and add result HTML to DOM.
 */
jQuery.flickrSearch = function(query) {
    var address = 'flickr/search/' + query.replace(" ", "+");

    $.ajax({
        url: address,
        success: function(data) {
            if (data != "") {
                html = "<div class=\"free\">" + data + "</div>";

                $('#flickr').append(html);
            }
        }
    });
    
}

$(document).ready(function(){

    // bind investigation click event to workflow
    //  1 - load Investigation search terms
    //  2 - search Freebase for Investigation search terms and add results to DOM
    //  3 - search Flickr for Investigation search terms and add results to DOM
    $('.investigation').click(function() {
        var features = $.investigationSearch($(this).attr('title')).split("#");
        $('#freebase').html("<h1>Freebase results for " + $(this).attr('title') + "</h1>");
        $('#flickr').html("<h1>Flickr results for " + $(this).attr('title') + "</h1>");
        var i = 0;
        while (features[i] != undefined || i < 20) {
            var feature = features[i];
            $.freebaseSearch(feature);
            $.flickrSearch(feature);
            
            i = i + 1;
        }
    });

    // datatables controller for dynamic Investigation table
    $('#table').dataTable( {
        "bPaginate" : true,
        "bFilter": true,
        "bSort": true,
        "bStateSave": true,
        "iDisplayLength": 10,
        "oLanguage": {
            "sZeroRecords": "No results to display",
            "sLengthMenu": 'Display <select>'+
        '<option value="20">20</option>'+
        '<option value="50">50</option>'+
        '<option value="-1">All</option>'+
        '</select> results'
        },
        "bLengthChange" : true,
        "bAutoWidth" : true,
        "sDom": '<"top"lfp<"clear">>rt<"bottom"ip<"clear">>'
    });

});